﻿package com.chinamobile.errorfind.bitmap;

import java.io.IOException;
import java.io.InputStream;

import com.tongji.android.R.drawable;

import android.content.pm.ApplicationInfo;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

public class BitmapReader {
	/**
	 * 读取图片资源
	 */
	public static Bitmap read(Resources resources, int resId) {
		BitmapDrawable  image = (BitmapDrawable) resources.getDrawable(resId);
		// 创建内存中的一张位图
		return image.getBitmap();
	}

	/**
	 * 读取图片资源
	 * 并将图片尺寸调整为width × height大小
	 */
	public static Bitmap read(Resources resources, int resId, int width,
			int height) 
	{
		
		Drawable image = resources.getDrawable(resId);
		// 创建内存中的一张位图
		Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
		Canvas canvas = new Canvas(bitmap);
		// drawable图片大小调整
		image.setBounds(0, 0, width, height);
		// drawable图片画到画布上
		image.draw(canvas);
		image=null;
		canvas=null;
		return bitmap;
		
	}
	public static Bitmap readfromAsset(AssetManager am, String filename , int width,
			int height) 
	{
		Bitmap image = null;
		try {
			InputStream is = am.open(filename);
			Bitmap b1 = BitmapFactory.decodeStream(is);
			is.close();
			Drawable drawable = new BitmapDrawable(b1);
			image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
			Canvas canvas = new Canvas(image);
			// drawable图片大小调整
			drawable.setBounds(0, 0, width, height);
			// drawable图片画到画布上
			drawable.draw(canvas);
			b1.recycle();
			b1 = null;
			drawable=null;
			canvas=null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return image;
		
	}

}
